package com.example.front.controller;

import com.alibaba.fastjson.JSONArray;
import com.example.common.dto.JsonResult;
import com.example.common.dto.ListDto;
import com.example.common.dto.Pagination;
import com.example.common.exception.BusinessException;
import com.example.front.dto.*;
import com.example.front.entity.Factory;
import com.example.front.serivce.FactoryService;
import com.example.front.serivce.WorkshopService;
import com.example.interceptor.UserContext;
import com.example.passport.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "严重等级")
@RestController
@CrossOrigin
@RequestMapping("/front/factory")
public class FactoryController {

    @Resource
    FactoryService factoryService;

    @ApiOperation("添加工厂")
    @PostMapping("/create")
    public JsonResult<String> create(@Valid Factory factory) {

        factoryService.createFactory(factory);

        return new JsonResult<>(true, "添加成功");
    }

    @ApiOperation("列出工厂列表")
    @GetMapping("/list")
    public JsonResult<ListDto<Factory>> list(@Valid Factory condition, Pagination pagination) {
        User user=UserContext.getUser();
        condition.setTenantId(UserContext.getUser().getTenantId());
        ListDto<Factory> factoryList = factoryService.listFactory(condition, pagination);
        return new JsonResult<>(factoryList);
    }

    @ApiOperation("修改数据")
    @PostMapping("/update")
    public JsonResult<String> update(@Valid Factory factory) {
        factoryService.update(factory);
        return new JsonResult<>();
    }

    @ApiOperation("删除工厂")
    @PostMapping("/delete")
    public JsonResult<String> delete(long id) {
        factoryService.deleteById(id,UserContext.getUser().getTenantId());
        return new JsonResult<>();
    }
}
